﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Linq;
using System.Windows.Media.Imaging;
using System.Collections;
using System.ComponentModel;

namespace Cinamon {
  public partial class MainPage : PhoneApplicationPage {
    List<Movie> noDuplicates;
    List<Movie> noSoonDuplicates;
    // Constructor
    

    public MainPage() {
      InitializeComponent();
      BackgroundWorker bw3 = new BackgroundWorker();

      bw3.DoWork += (s, e) => {
        WebClient wc = new WebClient();
        wc.DownloadStringCompleted += wc_DownloadStringCompleted;
        wc.DownloadStringAsync(new Uri("http://www.cinamon.ee/rss/schedule/1001.xml", UriKind.Absolute));
      };

      bw3.RunWorkerAsync();


    }
    void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e) {
      if (e.Error != null)
        return;
      XDocument scheduleXml = XDocument.Parse(e.Result);

      (App.Current as App).movies = (from query in scheduleXml.Descendants("schedule").Descendants("shows").Descendants("show")
                   where DateTime.Parse(query.Element("showDateTime").Value).CompareTo(DateTime.Now) >= 0
                   select new Movie() {
                     MoviePicture = (string)query.Element("images").Element("imageType2") ?? "Default.png",
                     MovieName = (string)query.Element("title"),
                     MovieLink = (string)query.Element("movieLink"),
                     MovieBuyLink = (string)query.Element("showLink"),
                     MovieDateTime = DateTime.Parse(query.Element("showDateTime").Value),
                     MovieId = (string)query.Element("movieId"),
                     MovieSoonest = DateTime.Parse(query.Element("showDateTime").Value).ToString("H:mm"),
                     MovieRating = (string)query.Element("ratingName"),
                     MovieDescription = (string)query.Element("showDescription"),
                     MovieLength = (string)query.Element("showDuration"),
                     MovieDateString = DateTime.Parse((string)query.Element("showDateTime")).ToString() 
                   }).ToList();

      noDuplicates = (App.Current as App).movies.Distinct(new MovieComparer()).ToList();
      foreach (Movie noDuplicate1 in noDuplicates) {
        Movie noDuplicate = noDuplicate1;
        if (noDuplicate.MovieDateTime.Date.Equals(DateTime.Now.Date) &&
            noDuplicate.MovieDateTime.TimeOfDay.CompareTo(DateTime.Now.TimeOfDay) > 0) {
          System.Threading.Thread.Sleep(200);
          Dispatcher.BeginInvoke(() => {
            todayBox.Items.Add(noDuplicate);
          });
        }
        if (noDuplicate.MovieDateTime.CompareTo(DateTime.Now) >= 0) {
          System.Threading.Thread.Sleep(200);
          Dispatcher.BeginInvoke(() => {
            allBox.Items.Add(noDuplicate);
          });
        }
      }

      (App.Current as App).comingMovies = (from query in scheduleXml.Descendants("schedule").Descendants("comingSoon").Descendants("movie")
                       select new Movie() {
                         MoviePicture = (string)query.Element("images").Element("imageType2") ?? "Default.png",
                         MovieName = (string)query.Element("title"),
                         MovieLink = (string)query.Element("movieLink"),
                         MovieId = (string)query.Element("movieId"),
                         MovieSoonest = "",//DateTime.Parse(query.Element("dayPremiere").Value).ToString("H:mm"),
                         MovieRating = (string)query.Element("ratingName"),
                         MovieDescription = (string)query.Element("movieDescription"),
                         MovieLength = (string)query.Element("movieDuration"),
                         MovieDateString = DateTime.Parse((string)query.Element("dayPremiere")).ToString() ?? String.Empty
                       }).ToList();

      noSoonDuplicates = (App.Current as App).comingMovies.Distinct(new MovieComparer()).ToList();
      foreach(Movie noSoonDuplicate1 in noSoonDuplicates) {
        Movie noSoonDuplicate = noSoonDuplicate1;
        System.Threading.Thread.Sleep(200);
        Dispatcher.BeginInvoke(() => {
          soonBox.Items.Add(noSoonDuplicate);
        });
      }

    }

    private void hyperLinkButton_Tap(object sender, GestureEventArgs e) {
      HyperlinkButton todayTap = sender as HyperlinkButton;
      NavigationService.Navigate(new Uri("/MovieTimeSelectPage.xaml?movieId=" + todayTap.CommandParameter.ToString(), UriKind.Relative));
    }

    private void hyperLinkButton_Soon_Tap(object sender, GestureEventArgs e) {
      HyperlinkButton soonTap = sender as HyperlinkButton;
      NavigationService.Navigate(new Uri("/MovieTimeSelectPage.xaml?comingSoonMovieId=" + soonTap.CommandParameter.ToString(), UriKind.Relative));
    }



  }
}